﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Invoicing.Model.Financial;
using Invoicing.Model.Members;
namespace Invoicing.Command.Financial
{
    public class VBankWaterCommand
    {
        public static VBankWaterCollection GetList(VBankWaterQuery sea)
        {
            VBankWaterCollection result = new VBankWaterCollection();
            using (DBCentre db = new DBCentre())
            {
                IQueryable<VBankWater> q = db.DataContext.VBankWaters;
                if (sea != null)
                {
                    if (sea.BankID > 0)
                        q = q.Where(p => p.BankID == sea.BankID);
                }
                var n = q.Count();
                if (n > 0)
                {
                    q = q.OrderByDescending(p => p.ID);
                    if (sea != null && sea.PageSize > 0)
                        q = q.Skip(sea.PageSize * sea.PageIndex).Take(sea.PageSize);
                    var lst = q.ToList();
                    result = Convertor.ConvertCollection<VBankWaterModel, VBankWater, VBankWaterCollection>(lst);
                    result.TotalRecordsCount = n;
                    foreach (var r in result)
                    {
                        if (sea.GetCustomer)
                        {
                            var cu = db.DataContext.Customers.FirstOrDefault(p => p.ID == r.CustomerID);
                            r.Customer = Convertor.ConvertTo<CustomerModel, Customer>(cu);
                        }
                        if (sea.GetBank)
                        {
                            var Bank = db.DataContext.Banks.FirstOrDefault(p => p.ID == r.BankID);
                            r.Bank = Convertor.ConvertTo<BankModel, Bank>(Bank);
                        }
                    }
                }
            }
            return result;
        }
    }
}
